﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc; 
using Admin.Filters;
using Data.Video; 
namespace Admin.Controllers
{
    [Authorize(Roles = "Admin,SubAdmin,GM,Content")]
    [InitializeSimpleMembership]
    public class CateController : Controller
    {
        private VideoEntities db = new VideoEntities();

        Htmls html = new Htmls();
        //
        // GET: /Cate/


        public ActionResult Index(int? id)
        { 
            ViewBag.id = id;
            return View(db.Category.ToList());
        }

        //
        // GET: /Cate/Details/5

        public ActionResult Details(int id = 0)
        {
            Category category = db.Category.Find(id);
            if (category == null)
            {
                return HttpNotFound();
            }
            return View(category);
        }

        //
        // GET: /Cate/Create

        public ActionResult Create(int? id)
        {
            ViewBag.Parent = html.SelectListCate(id, true);
            return View();
        }

        //
        // POST: /Cate/Create

        [HttpPost]
        public ActionResult Create(Category category)
        {
            if (ModelState.IsValid)
            {
                category.Level = "";
                category.Image = "";
                category.Type = 0;
                db.Category.Add(category);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.Parent = html.SelectListCate(category.Parent, true);
            return View(category);
        }

        //
        // GET: /Cate/Edit/5

        public ActionResult Edit(int id = 0)
        {
           
            Category category = db.Category.Find(id);
            if (category == null)
            {
                return HttpNotFound();
            }
            ViewBag.Parent = html.SelectListCate(category.Parent, true);
            return View(category);
        }

        //
        // POST: /Cate/Edit/5

        [HttpPost]
        public ActionResult Edit(Category category)
        {
            if (ModelState.IsValid)
            {
                Category categorys = db.Category.Find(category.ID);
                categorys.Name = category.Name;
                categorys.Parent = category.Parent;
                categorys.Status = category.Status;


                db.Entry(categorys).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.Parent = html.SelectListCate(category.Parent, true);
            return View(category);
        }

        //
        // GET: /Cate/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Category category = db.Category.Find(id);
            if (category == null)
            {
                return HttpNotFound();
            }
            return View(category);
        }

        //
        // POST: /Cate/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Category category = db.Category.Find(id);
            db.Category.Remove(category);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}